#coding:utf8
import cherrypy
#import datetime
from ConnectMSSQL import ms

from head import print_head
from end import print_end

from utf8code import *
from xxtj import *


_js = """
        <script type="text/javascript">
            $(function()
            {				
                $("#savebut").click(function()
                {
                    var lxsx = $("#lxsxtext").val();
                    var sblx = $("#sblxtext").val();
                    var xmlhttp;
                    if (window.XMLHttpRequest)
                    {// code for IE7+, Firefox, Chrome, Opera, Safari
                      xmlhttp=new XMLHttpRequest();
                    }
                    else
                    {// code for IE6, IE5
                      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                
                    xmlhttp.onreadystatechange=function()
                    {
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            if(xmlhttp.responseText)
                            {
                                alert(xmlhttp.responseText);
                            }
                            if(xmlhttp.responseText == "保存成功!*^_^*")
                            {
                                window.location.href="/sbxz";
                            }
                        }
                    }
                    xmlhttp.open("GET","/sblx/addSBLX?lxsx="+lxsx +"&sblx="+sblx,true);
                    xmlhttp.send();
                });

                $("#quitbut").click(function()
                {
                   window.location.href="/sbxz";
                });
            });
        </script>
"""
_top = """
	<form method="get">
	      <div id="div_operate">
	        <table width="auto">
              <tr>
                <th class="class_th" scope="col">序号</th>
                <th class="class_th">
                    类型缩写
                    <font>【设备编码前的三个标识符】</font>
                </th>
                <th class="class_th">设备类型</th>
                <th class="class_th">操作</th>
              </tr>
	          <tr>
                <td>0</td>
                <td><input type="text" id="lxsxtext" /></td>
	            <td><input type="text" id="sblxtext" /></td>
                <td class="class_td_opt">
                    <input type="button" id="savebut" value="保存" />
                    <input type="button" id="quitbut" value="取消" />
                </td>
	          </tr>
"""
_end = """
            </table>
        </div>
    </form>
"""

class SBLX:
    _mid_init = """            
    <tr class="class_tr">
        <td>%s</td>
        <td id="%s">%s</td>
        <td id="%s">%s</td>
        <td></td>
    </tr>
    """
    @cherrypy.expose
    def index(self):
        sql = """
        SELECT * FROM T_SBLX
        ORDER BY SBLX
        """
        dataList = ms.ExecQuery(sql)
        _mid = ''
        i = 0
        for sblx in dataList:
            i = i + 1
            _mid = _mid + self._mid_init % (i,1,sblx[0],2,sblx[1])
    	return utf8code(print_head % ('设备类型新增','新增设备类型') + _js + _top + _mid + _end + div_xxtj % (cherrypy.session.get('user'),len(dataList)) + print_end)
    @cherrypy.expose
    def addSBLX(self,lxsx,sblx):
        if (len(lxsx) == 3 and len(sblx) > 0):
            sql_check = "select sblx from t_sblx where lxsx = '%s' or sblx = '%s'" % (lxsx,sblx)
            listLX = ms.ExecQuery(sql_check)
            #判断执行的sql返回是否有值
            if listLX:
                return utf8code('保存失败！已存在【%s】类型或相同的类型缩写！' % listLX[0])#有值表示已存在该设备类型
            else:
                sql_addsblx = "insert into t_sblx values('%s','%s');" % (lxsx,sblx)
                ms.ExecNonQuery(sql_addsblx)
                return '保存成功!*^_^*'#返回无值表示
        else:
            return '保存失败！上两项必填，且缩写只能有3个字符！'